package com.hd.service.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.WorkCc;
import com.hd.entity.enums.WorkccStatus;
import com.hd.service.WorkCcService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("workCcService")
public class WorkCcServiceImpl implements WorkCcService {
    @Autowired
    private BaseDao<WorkCc,String> workCcDao;
    @Override
    public void save(WorkCc workCc) {
        workCc.setId(StringUtils.getUUId());
        workCc.setStatus(WorkccStatus.ON.getStatus());
        workCc.setCreateTime(new Date());
        workCcDao.save(workCc);
    }

    /**
     * 更新
     * @param objId
     * @param status
     */
    @Override
    public void update(String objId, Integer status,String type) {
        String hql="update WorkCc set status=? where objId=? and type=?";
        workCcDao.execute(hql,status,objId,type);
    }

    @Override
    public void updateById(String id, Integer status) {
        String hql="update WorkCc set status=?,readTime=? where id=?";
        workCcDao.execute(hql,status,new Date(),id);
    }

    @Override
    public void deleteByParams(String objId, String type) {
        String hql="delete from  WorkCc  where objId=? and type=?";
        workCcDao.execute(hql,objId,type);
    }

    @Override
    public void deleteByParams(String objId, String type,String userid) {
        String hql="delete from  WorkCc  where objId=? and type=? and userid=?";
        workCcDao.execute(hql,objId,type,userid);
    }


    /**
     * 分页查询
     * @param info
     * @param workCc
     */
    @Override
    public void selectDataGrid(PageInfo info, WorkCc workCc) {
        Map<String,Object> condition=info.getCondition();
        List<Object> params=new ArrayList<Object>();
        String hql="from WorkCc where 1=1";
        if(StringUtils.isNotNull(workCc.getUserId())){
            hql+=" and userId=?";
            params.add(workCc.getUserId());
        }
        if(StringUtils.isNotNull(workCc.getTitle())){
            hql+=" and title like ?";
            params.add("%"+workCc.getTitle().trim()+"%");
        }
        if(StringUtils.isNotNull(condition.get("createTimeStart"))){
            hql+=" and createTime>=?";
            params.add(condition.get("createTimeStart"));
        }
        if(StringUtils.isNotNull(condition.get("createTimeEnd"))){
            hql+=" and createTime<=?";
            params.add(condition.get("createTimeEnd"));
        }
        if(StringUtils.isNotNull(workCc.getStatus())){
            hql+=" and status=?";
            params.add(workCc.getStatus());
        }
        hql+=" order by createTime desc";
        String countHql="select count(*) "+hql;
        List<WorkCc>WorkccList=workCcDao.findPage(hql,info.getNowpage(),info.getPagesize(),params);
        Long count=workCcDao.getCountByList(countHql,params);
        info.setRows(WorkccList);
        info.setTotal(Integer.parseInt(count+""));
    }

    @Override
    public WorkCc get(String id) {
        return workCcDao.get(WorkCc.class,id);
    }
}
